<?php
/**
 * Created by PhpStorm.
 * User: woody
 * Date: 17-2-16
 * Time: 下午10:46
 */

namespace App\Http\Controllers\Admin;

use App\Models\Examples;
use Illuminate\Http\Request;


class ExamplesController extends AdminController {

    public function __construct()
    {
        parent::__construct();
        $this->_response['_active']['_model']       = 'examples';
        $this->_response['_active']['_action']      = '';
        $this->_response['_title']                  = '小一农货-装修案例管理';
    }
    public function index(Request $request){
        $page = $request->get('page', 1);
        $search = $request->all();
        $this->_response['_active']['_action']      = 'list';

        $ExamplesModel = new Examples();
        $data = $ExamplesModel->getData($search, $page, 20);

        $this->_response['examples'] = $data;
        $this->_response['examples']['pageData']['page'] = $page;
        $this->_response['examples']['pageData']['pageHtml'] = self::getPageHtml($page , $this->_response['examples']['pageData']['lastPage'] , '/admin/examples?');

        return view('admin.examples.list', $this->_response);
    }

    //具体案例 传入： id 返回
    public function getdata($id){
        $search['id']=$id;
        $ExamplesModel = new Examples();
        $data= $ExamplesModel->getData($search,1, 20);
        $this->_response['examples'] = $data;
        if(!empty($data)){
            return response()->json(['code' => 0, 'msg' => '返回成功','data'=>$this->_response['examples']]);

        }else{
            return response()->json(['code' => -1, 'msg' => '返回失败']);
        }
    }

    public function add(){
        $this->_response['_active']['_action']      = 'add';
        return view('admin.examples.add', $this->_response);
    }

    public function doAdd(\App\Http\Requests\Examples $request){
        $data = $request->all();
//        print_r($data);
//        exit;
   //保存图片
        $examplesModel = new Examples();
        foreach($data['img'] as $key=>$value){
            $saveImg = [
                'img'=>Config('app.set.url').$value['imgUrl'],
                'example_id'=>$data['user_id'],
            ];

          $imginfo=$examplesModel->saveImg($saveImg);

        }

  //将传过来的第一张图片作为背景图
        $saveData = [
            'backgroundImage'=>Config('app.set.url').$data['img'][0]['imgUrl'],
            'description' => $data['description'],
            'user_id'=>$data['user_id'],
            'area' => $data['area'],
            'created_at' => date('Y-m-d H:i:s', time())
        ];


        if($examplesModel->saveData($saveData)&&!empty($imginfo)){
            return response()->json(['code' => 0, 'msg' => '添加成功']);
           // return redirect("/admin/examples");
        }else{
            return response()->json(['code' => -1, 'msg' => '添加失败']);
        }
    }

    public function edit($id){
        $examplesModel = new Examples();
        $this->_response['examples'] = Examples::where('user_id', $id)->first();
        $this->_response['example_img'] = $examplesModel->getDatalist($id);
        //  return $this->_response;
         return view('admin.examples.edit', $this->_response);
    }

    public function doedit(\App\Http\Requests\Examples $request){
        $data = $request->all();
//         print_r($data);
//          exit;
        $saveData =[
            'backgroundImage' => $data['img'],
            'description' => $data['description'],
            'redirect_url'=>$data['redirect_url'],
            'area' => $data['area'],
            'updated_at' => date('Y-m-d H:i:s', time())
        ];

        $examplesModel = new Examples();
        if($examplesModel->setData($data['id'],$saveData)){
            return response()->json(['code' => 0, 'msg' => '修改成功']);

        }else{
            return response()->json(['code' => -1, 'msg' => '修改失败']);
        }
    }

   //装修图片管理
    public  function example_img($id){
        $examplemodel=new Examples();
        $data = $examplemodel->getDataimg($id);
        $this->_response['examples'] = Examples::where('user_id', $id)->first();
        $this->_response['example_img'] = $data;
       //  return $this->_response;
        return view('admin.examples.img', $this->_response);

    }

//添加案例图集

    public function addimg($id){
        $this->_response['examples'] = Examples::where('user_id', $id)->first();
        return view('admin.examples.addimg', $this->_response);
    }

    public function doAddimg(\App\Http\Requests\Examples_img $request){
        $data = $request->all();
//        print_r($data);
//        exit;
        //保存图片
        $examplesModel = new Examples();
        foreach($data['img'] as $key=>$value){
            $saveImg = [
                'img'=>Config('app.set.url').$value['imgUrl'],
                'example_id'=>$data['user_id'],
                'created_at' => date('Y-m-d H:i:s', time())
            ];

            $imginfo=$examplesModel->saveImg($saveImg);

        }

        if(!empty($imginfo)){
            return response()->json(['code' => 0, 'msg' => '添加成功']);
            // return redirect("/admin/examples");
        }else{
            return response()->json(['code' => -1, 'msg' => '添加失败']);
        }
    }

    //删除图集

    public function deleteimg($id){
        $examplesModel = new Examples();
        $data=[
            'deleted_at'=>date('Y-m-d H:i:s',time())
        ];
        if($examplesModel->delDataimg($id,$data)){
            return response()->json(['code' => 0, 'msg' => '删除成功']);
            // return redirect('/admin/examples');
        }else{
            return response()->json(['code' => -1, 'msg' => '删除失败']);
        }
    }


    public function delete($id){
        $examplesModel = new Examples();
        $data=[
            'deleted_at'=>date('Y-m-d H:i:s',time())
        ];
        if($examplesModel->delDataimg($id,$data)){
            return response()->json(['code' => 0, 'msg' => '删除成功']);
           // return redirect('/admin/examples');
        }else{
            return response()->json(['code' => -1, 'msg' => '删除失败']);
        }
    }



}